
package com.bluelinelabs.conductor.conductor.changehandler;

import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

import com.bluelinelabs.conductor.conductor.ControllerChangeHandler;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class FadeChangeHandler extends AnimatorChangeHandler {

    public FadeChangeHandler() {
    }

    public FadeChangeHandler(boolean removesFromViewOnPush) {
        super(removesFromViewOnPush);
    }

    public FadeChangeHandler(long duration) {
        super(duration);
    }

    public FadeChangeHandler(long duration, boolean removesFromViewOnPush) {
        super(duration, removesFromViewOnPush);
    }

    @Override
    protected AnimatorGroup getAnimator(ComponentContainer container, Component from, Component to, boolean isPush, boolean toAddedToContainer) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        AnimatorProperty fromAnimatorProperty = null;
        AnimatorProperty toAnimatorProperty = null;

        if (to != null) {
            float start = toAddedToContainer ? 0 : to.getAlpha();
            toAnimatorProperty = to.createAnimatorProperty();
            toAnimatorProperty.alphaFrom(start).alpha(1);
        }

        if (from != null && (!isPush || removesFromViewOnPush())) {
            fromAnimatorProperty = from.createAnimatorProperty();
            fromAnimatorProperty.alpha(0);
        }

        if (fromAnimatorProperty != null && toAnimatorProperty != null) {
            animatorGroup.runParallel(fromAnimatorProperty, toAnimatorProperty);
        } else if (fromAnimatorProperty != null) {
            animatorGroup.runParallel(fromAnimatorProperty);
        } else if (toAnimatorProperty != null) {
            animatorGroup.runParallel(toAnimatorProperty);
        }

        return animatorGroup;
    }

    @Override
    protected void resetFromView(Component from) {
        from.setAlpha(1);
    }

    @Override
    public ControllerChangeHandler copy() {
        return new FadeChangeHandler(getAnimationDuration(), removesFromViewOnPush());
    }
}
